﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： CarGroupRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-10-11
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper.Extensions;
using YouWei.Infrastructure.Models;
using Dapper;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    ///群组 仓储层
    /// </summary>	
    public class CarGroupRepository : BaseRepository<CarGroup>
    {

        #region 构造

        public CarGroupRepository() : base() { }

        public CarGroupRepository(string connectionString) : base(connectionString) { }

        public CarGroupRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        public Page<CarGroup> Pages(int pageSize, int pageIndex)
        {
            string sql = @"  SELECT T.*,T1.locationName AS CityName FROM CarGroup T WITH(NOLOCK)
                              LEFT JOIN BaseLocation_All T1 WITH(NOLOCK) ON T.CityID = T1.locationId
                              WHERE T.IsActive = 1";
            return Pages(pageIndex, pageSize, sql);
        }

        public Page<CarGroup> Pages(CarGroupSearchModel conditon)
        {
            string sql = @"  SELECT T.*,T1.locationName AS CityName FROM CarGroup T WITH(NOLOCK)
                              LEFT JOIN BaseLocation_All T1 WITH(NOLOCK) ON T.CityID = T1.locationId
                              WHERE T.IsActive = 1";
            if (conditon.MasterBrandID > 0)
            {
                sql += " AND T.MasterBrandID=" + conditon.MasterBrandID;
            }
            if (conditon.BrandID > 0)
            {
                sql += " AND T.BrandID=" + conditon.BrandID;
            }
            if (conditon.CityID > 0)
            {
                sql += " AND T.CityID=" + conditon.CityID;
            }
            return Pages(conditon.PageIndex, conditon.PageSize, sql);
        }
        

        public CarGroup GetCarGroup(int id)
        {
            string sql = @"  SELECT T.*,T1.locationName AS CityName FROM CarGroup T WITH(NOLOCK)
                              LEFT JOIN BaseLocation_All T1 WITH(NOLOCK) ON T.CityID = T1.locationId
                              WHERE T.IsActive = 1 AND CarGroupID=" + id;
            return FirstOrDefault(sql);
        }

        public List<CarGroup> GetCarGroups(string id)
        {
            string sql = @"   SELECT T.* FROM CarGroup T WITH(NOLOCK)
                              WHERE T.IsActive = 1 AND CarGroupID IN (" + id + ")";
            return Query(sql);
        }

        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Page<CarGroupDto> GetPages(int pageIndex, long pageSize, ReqCarGroupCondition condition)
        {
            StringBuilder sql = new StringBuilder("SELECT c.*,l.locationName as CityName FROM CarGroup c WITH(NOLOCK) left Join BaseLocation_All l WITH(NOLOCK) ON c.CityID=l.locationid where c.IsActive =1");
            if (!string.IsNullOrWhiteSpace(condition.StartDate))
            {
                sql.Append(" AND c.CreateDate>@StartDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.EndDate))
            {
                sql.Append(" AND c.CreateDate<@EndDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.Name))
            {
                sql.Append(" AND CHARINDEX(@CarGroupName,CarGroupName) >0");
            }
            if (condition.BrandID > 0)
            {
                sql.Append(" AND c.CarGroupID IN(SELECT CarGroupID FROM CarGroupBrand WHERE CarGroupID=c.CarGroupID AND BrandID=@BrandID)");
            }
            if (condition.ModelID > 0)
            {
                sql.Append(" AND c.CarGroupID IN(SELECT CarGroupID FROM CarGroupBrand WHERE CarGroupID=c.CarGroupID AND BrandID=@ModelID)");
            }
            if (condition.CityID > 0)
            {
                sql.Append(" AND c.CityID=@CityID");
            }

            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@CarGroupName", condition.Name, System.Data.DbType.String);
            dparams.Add("@BrandID", condition.BrandID, System.Data.DbType.Int32);
            dparams.Add("@ModelID", condition.ModelID, System.Data.DbType.Int32);
            dparams.Add("@CityID", condition.CityID, System.Data.DbType.Int32);
            dparams.Add("@StartDate", condition.StartDate, System.Data.DbType.String);
            dparams.Add("@EndDate", condition.EndDate, System.Data.DbType.String);
            return dbContext.Connection.Page<CarGroupDto>(pageIndex, pageSize, sql.ToString(), dparams);
        }


        /// <summary>
        /// 查询群组详情
        /// </summary>
        /// <param name="carGroupId"></param>
        /// <returns></returns>
        public CarGroupDto GetInfo(int carGroupId)
        {
            StringBuilder sql = new StringBuilder("SELECT c.*,b.BrandName,s.SerialName,l.locationName as CityName,l.pName as ProvinceName FROM CarGroup c WITH(NOLOCK) left Join CarBrand b WITH(NOLOCK) on c.brandid=b.brandid left Join CarSerial s WITH(NOLOCK) ON c.modelid=s.SerialID left Join BaseLocation_All l WITH(NOLOCK) ON c.CityID=l.locationid where c.IsActive =1");
            sql.Append(" AND c.CarGroupId=@CarGroupId");
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@CarGroupId", carGroupId, System.Data.DbType.Int32);
            return dbContext.Connection.QuerySingle<CarGroupDto>(sql.ToString(), dparams);
        }

        /// <summary>
        /// 查询群组详情
        /// </summary>
        /// <param name="carGroupId"></param>
        /// <returns></returns>
        public CarGroup GetGroupByModel(int ModelID)
        {
            StringBuilder sql = new StringBuilder("SELECT * FROM CarGroup WITH(NOLOCK)");
            sql.Append(" WHERE ModelID=@ModelID");
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@ModelID", ModelID, System.Data.DbType.Int32);
            return dbContext.Connection.QueryFirstOrDefault<CarGroup>(sql.ToString(), dparams);
        }


    }
}